#include "smart_home.h"
#include "ui_smart_home.h"
smart_home::smart_home(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::smart_home)
{
    ui->setupUi(this);
    this->setWindowTitle("智能家具");

    this->server = new QTcpServer(this);
    this->socket = nullptr ;
    //初始页面
    ui->port_num->setText("20000") ;
    //启动服务器
    connect(ui->btn_start,&QPushButton::clicked,[=](){
        QString port = ui->port_num->text();
        unsigned int port_num = port.toUInt();
        if(port_num<9999)
        {
            QMessageBox::critical(this,"port","port被占用！");
            return ;
        }
        this->server->listen(QHostAddress::Any,port_num);//监听
        QMessageBox::information(this,"成功了","启动成功！");

        ui->btn_start->setEnabled(false);
        ui->port_num->setReadOnly(true);
    });
    //连接客户端，获取套接字
    connect(this->server,&QTcpServer::newConnection,this,[=](){
        ui->connet_to->setText("已连接");

        //获取通信套接字
        this->socket = this->server->nextPendingConnection();

        //收信息，处理信息，判断状态
        connect(this->socket,&QTcpSocket::readyRead,this,[=](){
            QByteArray msg = this->socket->readAll();
            qDebug()<<msg;
            if(msg == "灯1开启"){
                if(l1 == false)
                ui->btn_l1->click();

            }
            else if(msg == "灯1关闭"){
                if(l1 == true)
                ui->btn_l1->click();

            }
            else if(msg == "灯2关闭"){
                if(l3 == true)
                ui->btn_l3->click();

            }
            else if(msg == "灯2开启"){
                if(l3 == false)
                ui->btn_l3->click();

            }
            else if(msg == "空调开启"){
                if(f1 == false)
                ui->btn_f1->click();

            }
            else if(msg == "空调关闭"){
                if(f1 == true)
                ui->btn_f1->click();

            }
        });

        //断开连接
        connect(this->socket,&QTcpSocket::disconnected,this,[=](){
            ui->connet_to->setText("未连接");
            ui->btn_start->setEnabled(true);
            this->socket->close();
        });
    });



    l1=l3=f1= false;
    connect(ui->widget_2, &my_mouse::send_DBClick, ui->btn_l1,&QPushButton::click);
    connect(ui->btn_l1,&QPushButton::clicked,this,[=](){
        // 判断当前图片并更改
        if (l1)
        {
            ui->btn_l1->setText("开启");
            //发送开灯信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "灯1关闭";
                this->socket->write(msg.toUtf8());
            }
            ui->light_1->setPixmap(QPixmap(":/pic/图片/black.png"));
            l1 = false;
        }
        else
        {
            ui->btn_l1->setText("关闭");
            ui->light_1->setPixmap(QPixmap(":/pic/图片/bright.png"));
            l1 = true;
            //发送关灯信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "灯1开启";
                this->socket->write(msg.toUtf8());
            }
        }
    });

    connect(ui->widget_4, &my_mouse::send_DBClick, ui->btn_l3,&QPushButton::click);
    connect(ui->btn_l3,&QPushButton::clicked, this,[=](){
        // 判断当前图片并更改
        if (l3)
        {
            ui->btn_l3->setText("开启");
            //发送开灯信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "灯2关闭";
                this->socket->write(msg.toUtf8());
            }
            ui->light_3->setPixmap(QPixmap(":/pic/图片/black.png"));
            l3 = false;
        }
        else
        {
            ui->btn_l3->setText("关闭");
            //发送关灯信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "灯2开启";
                this->socket->write(msg.toUtf8());
            }
            ui->light_3->setPixmap(QPixmap(":/pic/图片/bright.png"));
            l3 = true;
        }
    });

    connect(this,&smart_home::closef1,ui->btn_f1,&QPushButton::click);
    connect(ui->widget_5, &my_mouse::send_DBClick, ui->btn_f1,&QPushButton::click);
    connect(ui->btn_f1,&QPushButton::clicked, this, [=](){
        // 判断当前图片并更改
        if (f1)
        {
            ui->btn_f1->setText("开启");
            //发送开空调信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "空调关闭";
                this->socket->write(msg.toUtf8());
            }
            ui->conditoin->setPixmap(QPixmap(":/pic/图片/shut.png"));
            f1 = false;
            // 停止定时器
            timer_f1->stop();
        }
        else
        {
            ui->btn_f1->setText("关闭");
            //发送关空调信息
            if(ui->connet_to->text() =="已连接"){
                QString msg = "空调开启";
                this->socket->write(msg.toUtf8());
            }
            ui->conditoin->setPixmap(QPixmap(":/pic/图片/open.png"));
            f1 = true;

            // 每秒减少0.5的定时器
            timer_f1->start(1000);
        }
    });
       // 初始化计时器
       timer_f1 = new QTimer(this);
       // 连接计时器的timeout信号到槽函数
       connect(ui->tempratrue,&QLineEdit::textChanged,this,&smart_home::openf1);
       connect(timer_f1, &QTimer::timeout, this, &smart_home::updateValueDown);

}

smart_home::~smart_home()
{
    delete ui;
}

void smart_home::openf1()
{
    // 当温度大于36时开启condition
    currentValue = ui->tempratrue->text().toDouble();
       if (currentValue >= 36)
       {
           f1 = false;
           ui->tempratrue->setReadOnly(true);
           emit closef1();
       }
}

void smart_home::updateValueDown()
{
    // 每秒减少0.5
    currentValue = ui->tempratrue->text().toDouble();
    currentValue -= 0.5;
    ui->tempratrue->setText(QString::number(currentValue));

    // 如果达到某个条件，可以停止定时器
    if (currentValue <= 16)
    {
        timer_f1->stop();
        ui->tempratrue->setReadOnly(false);
        emit closef1();
    }
}

